import { app, BrowserWindow , Menu} from 'electron'
import '../renderer/store'
import { exec } from 'child_process'

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow

let iShouldQuit = app.makeSingleInstance(() => {
  mainWindow.show();
  mainWindow.focus();
  return true;
});

if (iShouldQuit) {
  app.quit();
} else {

const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  Menu.setApplicationMenu(null)
  mainWindow = new BrowserWindow({
    height: 800,
    useContentSize: true,
    width: 1400,
    minHeight:800,
    minWidth:1400,
  })

  mainWindow.loadURL(winURL);
  // mainWindow.webContents.openDevTools();
  process.env.NODE_ENV === 'development' && mainWindow.webContents.openDevTools();
  runExec();

  mainWindow.on('closed', () => {
    closeExec();
    mainWindow = null;
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    closeExec();
    app.quit();
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

function runExec() {
  const cmdStr = process.env.NODE_ENV === 'development' ? `python python/config_tools1.py`: 'start /b ./resources/python/config_tools1.exe'
  exec(cmdStr, {});
}

function closeExec(){
  let cmd=require('node-cmd');
  cmd.run('taskkill -f -im config_tools1.exe');
}

}
/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */